package com.gx.smart.construction.ui.common.fragment;

import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.ToastUtils;
import com.gx.smart.base.BaseMVVMFragmentV2;
import com.gx.smart.construction.ui.common.model.AreaName;
import com.gx.smart.construction.ui.common.model.BuildingName;
import com.gx.smart.construction.ui.common.model.CommonItemLabel;
import com.gx.smart.construction.ui.common.model.RoomName;
import com.gx.smart.construction.ui.common.model.UnitName;
import com.gx.smart.construction.ui.common.viewmodel.BottomDialogUtil;
import com.gx.smart.construction.ui.common.viewmodel.CommonViewModel;
import com.gx.smart.lib.event.LoadingMessageEvent;
import com.gx.wisestone.constructionappgrpclib.grpc.organizationalstructure.ConstructionAppOrganizationalStructureDto;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: xiaosy
 * 时间: 2022/2/11
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public abstract class CommonSelectRoomFragment<T extends CommonViewModel> extends BaseMVVMFragmentV2<T> {
    private final List<AreaName> areaNameItems = new ArrayList<>();
    private final List<BuildingName> buildingNameItems = new ArrayList<>();
    private final List<UnitName> unitNameItems = new ArrayList<>();
    private Observer<List<ConstructionAppOrganizationalStructureDto>> areaListObserver;
    private Observer<List<ConstructionAppOrganizationalStructureDto>> buildingObserver;
    private Observer<List<ConstructionAppOrganizationalStructureDto>> unitObserver;

    public void selectArea() {
        if (areaListObserver == null) {
            areaListObserver = list -> {
                BottomDialogUtil.addConversionList(areaNameItems,new AreaName(), "分区为空", list);
                BottomDialogUtil.showBottomDialog(requireActivity(), areaNameItems, viewModel);
            };
        }
        viewModel.getAreaList(1).observe(getViewLifecycleOwner(), areaListObserver);
    }

    public void selectBuilding() {
        AreaName areaName = viewModel.getAreaName().getValue();
        if (areaName == null) {
            ToastUtils.showLong("请先选择分区");
            return;
        }
        if (buildingObserver == null) {
            buildingObserver = list -> {
                BottomDialogUtil.addConversionList(buildingNameItems,new BuildingName(), "楼栋为空", list);
                BottomDialogUtil.showBottomDialog(requireActivity(), buildingNameItems, viewModel);
            };
        }
        viewModel.getBuildList(1, areaName.getId()).observe(getViewLifecycleOwner(), buildingObserver);
    }


    public void selectUnit() {
        BuildingName buildingName = viewModel.getBuildingName().getValue();
        if (buildingName == null) {
            ToastUtils.showLong("请先选择楼栋");
            return;
        }
        if (unitObserver == null) {
            unitObserver = list -> {
                BottomDialogUtil.addConversionList(unitNameItems,new UnitName(), "单元为空", list);
                BottomDialogUtil.showBottomDialog(requireActivity(), unitNameItems, viewModel);
            };
        }
        EventBus.getDefault().post(new LoadingMessageEvent(true));
        viewModel.getUnitList(1, buildingName.getId()).observe(getViewLifecycleOwner(), unitObserver);
    }


    public boolean commonHandle() {
        AreaName areaName = viewModel.getAreaName().getValue();
        BuildingName buildingName = viewModel.getBuildingName().getValue();
        UnitName unitName = viewModel.getUnitName().getValue();
        RoomName roomName = viewModel.getRoomName().getValue();
        if (areaName == null) {
            ToastUtils.showLong("请选择分区");
            return false;
        }
        if (buildingName == null) {
            ToastUtils.showLong("请选择楼栋");
            return false;
        }
        if (unitName == null) {
            ToastUtils.showLong("请选择单元");
            return false;
        }
        if (roomName == null) {
            ToastUtils.showLong("请选择房间");
            return false;
        }
        return true;
    }
}
